#!/usr/bin/env python3
"""
主测试运行器
统一运行所有分类的测试
"""

import subprocess
import sys
import os
from pathlib import Path

def run_command(cmd, description):
    """运行命令并显示结果"""
    print(f"\n{'='*60}")
    print(f"🔄 {description}")
    print(f"{'='*60}")
    
    try:
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True, cwd=Path(__file__).parent.parent)
        
        if result.stdout:
            print(result.stdout)
        if result.stderr:
            print(f"⚠️ 警告/错误信息:\n{result.stderr}")
            
        if result.returncode == 0:
            print(f"✅ {description} - 成功")
            return True
        else:
            print(f"❌ {description} - 失败 (退出码: {result.returncode})")
            return False
            
    except Exception as e:
        print(f"❌ {description} - 异常: {str(e)}")
        return False

def main():
    """主函数"""
    print("🚀 开始运行所有测试...")
    
    # 检查是否在正确的目录
    if not os.path.exists('tests'):
        print("❌ 错误: 请在项目根目录运行此脚本")
        sys.exit(1)
    
    results = []
    
    # 1. 运行单元测试
    success = run_command("pytest tests/unit/ -v", "单元测试")
    results.append(("单元测试", success))
    
    # 2. 运行集成测试
    success = run_command("pytest tests/integration/ -v", "集成测试")
    results.append(("集成测试", success))
    
    # 3. 运行Mock数据测试
    success = run_command("python tests/mock/run_mock_tests.py", "Mock数据测试")
    results.append(("Mock数据测试", success))
    
    # 4. 运行所有pytest测试（包括根目录的配置文件）
    success = run_command("pytest tests/ -v --tb=short", "完整测试套件")
    results.append(("完整测试套件", success))
    
    # 显示总结
    print(f"\n{'='*60}")
    print("📊 测试结果总结")
    print(f"{'='*60}")
    
    passed = 0
    failed = 0
    
    for test_name, success in results:
        status = "✅ 通过" if success else "❌ 失败"
        print(f"{test_name:<20} {status}")
        if success:
            passed += 1
        else:
            failed += 1
    
    print(f"\n总计: {passed + failed} 个测试套件")
    print(f"通过: {passed}")
    print(f"失败: {failed}")
    
    if failed == 0:
        print("\n🎉 所有测试都通过了!")
        return 0
    else:
        print(f"\n⚠️ 有 {failed} 个测试套件失败")
        return 1

if __name__ == '__main__':
    exit_code = main()
    sys.exit(exit_code) 